<template>
	<div class="export-dialog-view" v-loading="pageLoading">
		<div class="export-dialog">
			<div class="left-lay">
				<div class="split-title">{{ title }}</div>
				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.device") }}
					</div>
					<div class="line-value">{{ $store.state.btn_set_BHView2 ? $store.state.plantName : '担当課' }}</div>
				</div>

				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.object") }}
					</div>
					<div class="line-value">
						{{
		$store.state.macType == 1
			? $t("message.bhView.leftComponents.machine")
			: $t("message.bhView.leftComponents.piping")
	}}
					</div>
				</div>

				<div class="left-line">
					<div class="line-title">
						{{ $t("message.reportFrom.exportDialog.title_c") }}
					</div>
					<div class="line-value">{{ fileTypeStr }}</div>
				</div>

				<div class="left-draw" id="left-draw"></div>
			</div>

			<div class="tip-lay"></div>

			<div class="right-lay">
				<div class="split-title text-right">
					<i class="el-icon-close" @click="CloseExprotDiloag"></i>
				</div>
				<div class="split-title">
					{{ $t("message.reportFrom.exportDialog.title_l") }}
				</div>

				<div class="right-line">
					<div class="line-title">
						出力対象測定年月：
					</div>
					<div class="line-content">
						<el-date-picker type="month" :format="format" :value-format="format" placeholder=" "
							class="ipt picker orange" v-model="SearchForm">
						</el-date-picker>
						<span>~</span>
						<div class="line-content">
							<el-date-picker type="month" :format="format" :value-format="format" placeholder=" "
								class="ipt picker orange" v-model="SearchTo">
							</el-date-picker>
						</div>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						<el-checkbox v-model="PrintType">{{ $t("最小値に文字色を設定する") }}</el-checkbox>
					</div>
				</div>
				<div class="right-line">
					<div class="line-title">
						出力対象測定点：
					</div>
					<div class="line-content" style="padding-left:12px;">
						<el-radio-group v-model="Unit" @change="handleUnitChange">
							<el-radio :label="1" :disabled="isDisabled1">高圧ガス定点</el-radio>
						</el-radio-group>
					</div>
				</div>
				<div class="right-line">
					<div class="line-content" style="padding-left:134px;">
						<el-radio-group v-model="Unit" @change="handleUnitChange">
							<el-radio :label="2" :disabled="isDisabled2">高圧ガス定点以外</el-radio>
						</el-radio-group>
					</div>
				</div>
				<div class="right-line">
					<div class="line-content" style="padding-left:134px;">
						<el-radio-group v-model="Unit" @change="handleUnitChange">
							<el-radio :label="3">すべての測定点</el-radio>
						</el-radio-group>
					</div>
				</div>
			</div>
			<footer class="footer-view">
				<div class="">
					<div class="line-title" style="width: 100%">
						{{ $t("message.reportFrom.exportDialog.tip") }}
					</div>
				</div>
				<div>
					<button @click="CloseExprotDiloag">Cancel</button>
					<button @click="onExportClick">OK</button>
				</div>
			</footer>
		</div>
	</div>
</template>

<script>
import {
	ExReportList, 
	GetFirstAndCurrentDate,
	GetHighPressureGasFixedPointData,
	GetHighPressureGasExternalInfo

} from "@/api/api";
import { getLocalStorage } from "@/utils/storage";
import timeFormat from "@/utils/timeFormat";

export default {
	props: ["title", "fileTypeStr", "selectMacData", "fileRadio"],
	name: "B_B_Export",
	data() {
		return {
			SearchForm: '',
			SearchTo: '',
			pageLoading: false,
			format: timeFormat(),
			isDisabled1: false,
			isDisabled2: false,
			Unit: '',  
			PrintType: false 
		};
	},
	components: {},
	watch: {},
	methods: {
		CloseExprotDiloag() {
			this.$emit("CloseExprotDiloag");
		},
		async onExportClick() {
			this.pageLoading = true;
			let DmgCode = this.selectMacData.map(item => parseInt(item.macCode)); 
			let dataJson = {
				PltCode: this.selectMacData[0].pltCode,
				MacType: this.selectMacData[0].macType || this.$store.state.macType,
				DmgCode,
				SearchForm: this.SearchForm,
				SearchTo: this.SearchTo,
				Unit: this.Unit,
				PrintType: this.PrintType ? 2 : 1
			};
			let res = await ExReportList(dataJson);
			this.pageLoading = false;

			if (res.data.code == -310) {
				this.$message.info(res.data.msg);
				return;
			} else if (res.data.code != 0) {
				this.$alert(res.data.msg, '', {
					confirmButtonText: 'OK',
					type: 'warning',
					callback: action => {
					}
				})
				return
			}else if (res.data.code == 0) {
				window.open(res.data.data);
			}
			this.$emit("CloseExprotDiloag");
		},
		handleUnitChange(val) {
			console.log("Unit changed to: ", val);
		},
		async initFn() {
			this.pageLoading = true;
			try {
				const response = await GetFirstAndCurrentDate();
				if (response && response.data && response.data.code === 0) {
					this.SearchForm = response.data.data.opeBgn;
					this.SearchTo = response.data.data.opeEnd;
				} else {
				}
			} catch (error) {
			} finally {
				this.pageLoading = false;
			}

			let isDisabled1 = false;
			let isDisabled2 = false;

			try {
				const res1 = await GetHighPressureGasFixedPointData();
				const all1 = res1.data.code === 0 && res1.data.data.every(item => item.pointNo1 === '00');
				if (all1) {
					isDisabled1 = true;
				}
			} catch (error) {
			}

			try {
				const res2 = await GetHighPressureGasExternalInfo();
				const all2 = res2.data.code === 0 && res2.data.data.every(item => item.pointNo2 === '00');
				if (all2) {
					isDisabled2 = true;
				}
			} catch (error) {
			}

			if (isDisabled1 && isDisabled2) {
				this.Unit = 3;
			} else if (!isDisabled1) {
				this.Unit = 1;
			} else if (!isDisabled2) {
				this.Unit = 2;
			}

			this.isDisabled1 = isDisabled1;
			this.isDisabled2 = isDisabled2;
			this.pageLoading = false;
		},
		logPrintType() {
			console.log("PrintType value changed to: ", this.PrintType);
		}
	},
	created() {
		this.initFn();
	},
	mounted() {

	},
	beforeDestroy() {},
	destroyed() {},
};
</script>
<style lang="scss" scoped>
.export-dialog-view {
	position: fixed;
	width: 100%;
	height: 100%;
	left: 0;
	top: 0;
	z-index: 6;
}

.export-dialog {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 900px;
	height: 360px;
	background: #ffffff;
	box-shadow: 0px 12px 48px 16px rgba(0, 0, 0, 0.05),
		0px 9px 28px 0px rgba(0, 0, 0, 0.08),
		0px 6px 16px -8px rgba(0, 0, 0, 0.15);
	display: flex;
	flex-direction: row;
	padding: 0 12px;
}

.left-lay {
	position: relative;
	width: 40%;
	height: 100%;
	display: flex;
	flex-direction: column;

	.left-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 20px;
		padding-top: 10px;

		.line-title {
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
		}

		.line-value {
			font-size: 14px;
			font-weight: 400;
			color: #7b7b7b;
			line-height: 22px;
			margin-left: 10px;
		}
	}

	.left-draw {
		position: absolute;
		top: 100vh;
		width: 750px;
		height: 611px;
		background-color: bisque;
	}
}

.tip-lay {
	width: 7px;
	border-radius: 4px;
	background: #ccc;
	margin-top: 46px;
	margin: 46px 15px 85px 15px;
}

.right-lay {
	position: relative;
	width: 60%;
	height: 100%;
	display: flex;
	flex-direction: column;

	.el-icon-close {
		font-size: 16px;
		cursor: pointer;
		width: 46px;
		height: 45px;
		line-height: 45px;
	}

	.right-block-container {
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.text-right {
		padding: 0 !important;
		text-align: right;
	}

	.right-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 12px;
		padding-top: 10px;

		.line-title {
			min-width: 100px;
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
			text-align: left;
			margin-right: 10px;
		}

		.line-content {
			flex-grow: 1;
			flex-shrink: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
		}

		.ipt {
			width: 288px;
			height: 32px;

			::v-deep .el-input__inner {
				width: 100%;
				height: 32px;
				background-color: #fcdaaf;
			}

			::v-deep .el-input__inner:focus {
				border: 1px solid #dcdfe6;
			}

			::v-deep .el-input__icon {
				line-height: 32px;
				color: #909399;
			}

			::v-deep .el-input__suffix {
				width: 32px;
				right: 0;
				background: #dee0e3;
			}
		}

		.picker {
			width: 130px;

			::v-deep .el-input__inner {
				width: 100%;
			}

			::v-deep .el-input__suffix {
				background: transparent;
			}

		}

		span {
			padding: 0 5px;
			margin: 0 5px;
		}

		.divider {
			width: 100%;
			height: 1px;
			background-color: #f0f0f0;
		}
	}

	.right-line.bottom-border {
		padding-bottom: 10px;
	}

	.flex-row {
		flex-direction: row;
	}

	.flex-column {
		flex-direction: column;
	}
}

.unit-selection {
	margin: 10px 0;
}

.split-title {
	width: 100%;
	font-size: 16px;
	font-weight: 700;
	color: rgba(0, 0, 0, 0.85);
	line-height: 22px;
	padding: 12px 12px;
	border-bottom: 1px solid #f0f0f0;
}

.footer-view {
	width: calc(100% - 30px);
	box-sizing: border-box;
	position: absolute;
	z-index: 10;
	bottom: 16px;
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	padding-top: 17px;
	border-top: 1px solid #f0f0f0;
	font-size: 16px;
	font-weight: 700;

	button {
		width: 82px;
		height: 32px;
		background: #ffffff;
		border: 1px solid #d9d9d9;
		margin-left: 12px;
		border-radius: 4px;
		font-size: 14px;
		font-weight: 500;
	}

	button:nth-child(2) {
		background: #106ebe;
		color: #fff;
	}
}

#right-CAD {
	width: 1207px;
	height: 680px;
	position: fixed;
	top: -200vh;
	left: 0;
	z-index: -999;
}
</style>
